﻿@model IEnumerable<Navigation>
@{
    
    int maxDepth = ViewData.Get<int>("maxDepth", 0);
    IUser currentUser = UserContext.CurrentUser;
}
<div class="tn-box tn-widget tn-widget-content tn-corner-all tnc-sidebar">
    <div class="tn-box-content tn-widget-content tn-corner-all">
        <div class="tnc-menu-item">
            @if (Model != null && Model.Count() > 0)
            {

                bool hasChildren = Model.Where(n => n.Children != null && n.Children.Count() > 0).Count() > 0;
                int currentNavigationId = Html.ViewContext.RouteData.Values.Get<int>("CurrentNavigationId", 0);

                if (hasChildren)
                {
                    foreach (var nav in Model)
                    {
                        if (nav.Children.Count(c => c.IsControlPanelManageable(currentUser)) == 0)
                        {
                            continue;
                        }
                <div class="tn-content-heading tn-widget-header tn-corner-top tn-border-bottom">
                    <h4>
                        <span class="tn-icon-file"></span>@nav.Text</h4>
                </div>
                <ul class="tn-side-menu">
                    @GetNavigation(nav.Children.Where(p=>p.IsEnabled==true), currentNavigationId)
                </ul>
                    }
                }
                else
                {               
                <ul class="tn-side-menu">
                    @GetNavigation(Model, currentNavigationId)
                </ul>
                }
            }
        </div>
        <div class="tn-box-content tn-widget-content tn-corner-all">
            @*@Html.Action("_RecentVisit", "ControlPanel")*@
        </div>
    </div>
</div>
@helper GetNavigation(IEnumerable<Navigation> navigations, int currentNavigationId)
    { 
        if (navigations == null && navigations.Count() == 0)
        {
            return;
        }

        foreach (var navigation in navigations.OrderBy(n => n.DisplayOrder))
        {
            if (!navigation.IsControlPanelManageable(UserContext.CurrentUser))
            {
                continue;
            }

            string selectCalss = string.Empty;
            if (navigation.IsCurrent(currentNavigationId))
            {
                selectCalss = "class=tnc-sublist-over";
            }
            
    <li @(selectCalss)><em></em><a class="tn-menu-text" href="@navigation.GetUrl()" target="@navigation.NavigationTarget">@navigation.Text</a></li>
        }
}